var changePhone = {};
changePhone.methods = {};

$(function(){
    // 手机号四位隐藏
    changePhone.methods.phoneTxtHide();

    // 验证身份表单校验
    changePhone.methods.identityFormCheck();

    // 验证身份表单提交
    changePhone.methods.identityFormSubmit();

    // 绑定手机表单校验
    changePhone.methods.phoneFormCheck();

    // 绑定手机表单提交
    changePhone.methods.phoneFormSubmit();

    // 验证身份验证码
    changePhone.methods.codeBtn($(".shop-identity-form .shop-code-btn"));

    // 绑定手机验证码
    changePhone.methods.codeBtn($(".shop-phone-form .shop-code-btn"));

})

// 手机号四位隐藏
changePhone.methods.phoneTxtHide = function(){
    var phoneTxtFirst = $(".shop-identity-form .shop-phone-txt").text().substring(0,3);
    var phoneTxtLast = $(".shop-identity-form .shop-phone-txt").text().substring(7,11);
    $(".shop-identity-form .shop-phone-txt").text(phoneTxtFirst + "****" + phoneTxtLast);
}

// 验证身份表单校验
changePhone.methods.identityFormCheck = function(){
    //验证规则
    $(".shop-identity-form").validate({

        rules:{
            codeNumber1:{
                required:true,
                minlength:4,
                maxlength:4
            }
        },
        messages:{
            codeNumber1:{
                required:"请输入短信验证码",
                minlength:"验证码为四位",
                maxlength:"验证码为四位"
            }
        }
    });
}

// 验证身份表单提交
changePhone.methods.identityFormSubmit = function(){
    $(".shop-identity-form").validate({
        submitHandler:function(form){
            $(form).ajaxSubmit();
        }
    })  

    $(".shop-identity-form .shop-identity-btn").on("click",function(){
        if($(".shop-identity-form").valid()){
            $(".shop-account-phone-form").hide();
            $(".shop-phone-form").show();
            $(".shop-account-phone-title .shop-phone").addClass("shop-sel");
        }
    })
}

// 绑定手机表单校验
changePhone.methods.phoneFormCheck = function(){
    //验证规则
    $(".shop-phone-form").validate({
        rules:{
            phoneNumber:{
                required:true,
                isMobile : true
            },
            codeNumber2:{
                required:true,
                minlength:4,
                maxlength:4
            }
        },
        messages:{
            phoneNumber:{
                required:"请输入手机号",
                isMobile:"请正确填写手机号码" 
            },
            codeNumber2:{
                required:"请输入短信验证码",
                minlength:"验证码为四位",
                maxlength:"验证码为四位"
            }
        }
    });

    jQuery.validator.addMethod("isMobile", function(value, element) {  
        var length = value.length;  
        var mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;  
        return this.optional(element) || (length == 11 && mobile.test(value)); 
    }, "请正确填写手机号码"); 
}
// 绑定手机表单提交
changePhone.methods.phoneFormSubmit = function(){
    $(".shop-phone-form").validate({
        submitHandler:function(form){
            $(form).ajaxSubmit();
        }
    })  

    $(".shop-phone-form .shop-phone-btn").on("click",function(){
        if($(".shop-phone-form").valid()){
            $(".shop-account-phone-form").hide();
            $(".shop-account-complete").show();
            $(".shop-account-phone-title .shop-complete").addClass("shop-sel");
        }
    })
}



// 获取验证码公用
changePhone.methods.codeBtn = function(obj){
    var flag = true;
    obj.on("click", function () {
        // 启动定时器
        var alltime = 60 * 1000;
        if(flag){
            $(this).html("60s");
            flag = false;
            var timer = setInterval(function () {
                alltime = alltime - 1000;
                var str = parseInt(alltime / 1000) + "s";
                obj.html(str.toString());
                if (alltime <= 0) {
                    clearInterval(timer);
                    obj.html("获取验证码");
                    flag = true;
                }
            }, 1000)
        }
        
    });
}

